package io.sage.modules.camera.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import io.sage.modules.hcnet.service.HcNetRadioService;
import io.sage.modules.sys.entity.SysUserEntity;
import io.sage.modules.sys.shiro.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.sage.common.utils.R;
import io.sage.common.validator.ValidatorUtils;
import io.sage.modules.camera.entity.BMonitorCamera;
import io.sage.modules.camera.entity.BMonitorCameraExample;
import io.sage.modules.camera.service.BMonitorCameraService;
import io.swagger.annotations.ApiOperation;

/**
 * Created by Administrator on 2019/7/28 0028.
 */
@RestController
@RequestMapping("/monitor/bmonitorcamera")
public class MonitorCameraController {


    @Resource
    private BMonitorCameraService bMonitorCameraService;
    @Resource
    private HcNetRadioService hcNetRadioService;

    /**
     * 摄像头加播放的列表
     * @param param
     * @return
     */
    @RequestMapping("/lists")
    public R lists(@RequestParam Map<String,Object> param){

      List<BMonitorCamera> monitorCameraList = null;
      try {
          int wareHouseId = Integer.parseInt(param.get("wareHouseId").toString());
          BMonitorCameraExample bMonitorCameraExample = new BMonitorCameraExample();
          BMonitorCameraExample.Criteria criteria = bMonitorCameraExample.createCriteria();
          criteria.andWareHouseIdEqualTo(wareHouseId);
          monitorCameraList = bMonitorCameraService.selectByExample(bMonitorCameraExample);
      } catch (Exception e) {
          e.printStackTrace();
      }
      return R.ok().put("monitorCameraList", monitorCameraList);
  }

    
    /**
     * 摄像头全部列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("monitor:bmonitorcamera:list")
    public R list(@RequestParam Map<String,Object> params){
        Integer pageSize = 0;
        if (params != null && params.get("limit") != null) {
            pageSize = Integer.parseInt(params.get("limit").toString());
        }
        Integer pageNum = 10;
        if (params != null && params.get("page") != null) {
            pageNum = Integer.parseInt(params.get("page").toString());
        }
        HashMap<String, Object> paramMap = new HashMap<String, Object>();
        if(params.get("name")!=null){
            if(!"".equals(params.get("name").toString())){
                paramMap.put("name", params.get("name").toString());
            }
        }
        PageHelper.startPage(pageNum, pageSize);

        List<BMonitorCamera> monitorCameraList  = bMonitorCameraService.selectBMonitorCameraList(paramMap);

        PageInfo<BMonitorCamera> pageInfo = new PageInfo<BMonitorCamera>(monitorCameraList);
        return R.ok().put("monitorCameraList", pageInfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ApiOperation("保存摄像头")
    @RequiresPermissions("monitor:bmonitorcamera:save")
    public R save(@RequestBody JSONObject jsonObject){
        BMonitorCamera bMonitorCamera = jsonObject.getObject("bMonitorCamera", BMonitorCamera.class);
        ValidatorUtils.validateEntity(bMonitorCamera);
        //设置创建/修改时间/
        bMonitorCamera.setCreateTime(new Date());
        bMonitorCamera.setUpdateTime(new Date());
        //默认是否删除为1
        bMonitorCamera.setIsDel("1");
        bMonitorCameraService.insert(bMonitorCamera);
        return  R.ok();
    }
    /**
     * 修改
     * @return
     */
    @RequestMapping("/update")
    @ApiOperation("修改摄像头")
    @RequiresPermissions("monitor:bmonitorcamera:update")
    public R update(@RequestBody JSONObject jsonObject){
        BMonitorCamera bMonitorCamera = jsonObject.getObject("bMonitorCamera", BMonitorCamera.class);
        ValidatorUtils.validateEntity(bMonitorCamera);
        if(bMonitorCamera.getId()==null){
            return R.error("需要修改的id，不能为空");
        }
        bMonitorCamera.setUpdateTime(new Date());
        int update = bMonitorCameraService.updateByPrimaryKeySelective(bMonitorCamera);

        if(update>0){
            return R.ok();
        }
        return R.error();
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @ApiOperation("摄像头信息")
    @RequiresPermissions("monitor:bmonitorcamera:info")
    public R info(@PathVariable("id") Integer id){
        BMonitorCamera bMonitorCamera = bMonitorCameraService.selectByPrimaryKey(id);
        return R.ok().put("bMonitorCamera", bMonitorCamera);
    }
    /**
     * 信息
     */
    @RequestMapping("/catchPic/{id}")
    @ApiOperation("抓拍")
    @RequiresPermissions("monitor:bmonitorcamera:info")
    public R catchPic(@PathVariable("id") String id){
        System.out.println(id);
       SysUserEntity sysUserEntity= ShiroUtils.getUserEntity();
        BMonitorCamera bMonitorCamera = bMonitorCameraService.selectByPrimaryKey(Integer.parseInt(id));
        Map<String,Object> map=hcNetRadioService.catchPic(bMonitorCamera,sysUserEntity);

        return R.ok().put("date", map);
    }
    /**
     * 批量删除
     */
    @RequestMapping("/delete")
    @ApiOperation("刪除摄像头")
    @RequiresPermissions("monitor:bmonitorcamera:delete")
    public R delete(@RequestBody Integer[] ids){

        int delete = bMonitorCameraService.deleteBatchIds(ids);

        if(delete>0){
            return R.ok();
        }

        return R.error();
    }

}
